
find_package(QT NAMES Qt6 Qt5 COMPONENTS LinguistTools REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS LinguistTools REQUIRED)

set(TS_FILES)
set(QM_FILES)
set(TS_SOURCE_DIR)

## Source directory
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
    set(TS_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)
else()
    set(TS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif()

## Translation files
foreach (LANG ${LINGUAS})
    list(APPEND TS_FILES ${TS_SOURCE_DIR}/${PROJECT_NAME}_${LANG}.ts)
endforeach()
source_group("Translations" FILES ${TS_FILES})

## Configure .qm files output directory
# set_source_files_properties(${TS_FILES}
#     PROPERTIES OUTPUT_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}
# )

## Convert source files to absolute path
set(TS_SOURCE_FILES_ABS)
foreach(TS_SOURCE ${TS_SOURCE_FILES})
    get_filename_component(TS_SOURCE ${TS_SOURCE} ABSOLUTE)
    list(APPEND TS_SOURCE_FILES_ABS "${TS_SOURCE}")
endforeach()
set(TS_SOURCE_FILES ${TS_SOURCE_FILES_ABS})

## Generate translations
if(TRANSLATION_GEN)
    ## Generate .ts and .qm files
    if(NOT TS_SOURCE_FILES)
        file(GLOB_RECURSE TS_SOURCE_FILES
            RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
            ${CMAKE_CURRENT_SOURCE_DIR}/*.ui
            ${CMAKE_CURRENT_SOURCE_DIR}/*.h
            ${CMAKE_CURRENT_SOURCE_DIR}/*.c
            ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
            ${CMAKE_CURRENT_SOURCE_DIR}/*.cxx
            ${CMAKE_CURRENT_SOURCE_DIR}/*.c++
        )
    endif()
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt6_create_translation(QM_FILES ${TS_FILES} ${TS_SOURCE_FILES} OPTIONS -locations relative -no-sort -silent)
    else()
        qt5_create_translation(QM_FILES ${TS_FILES} ${TS_SOURCE_FILES} OPTIONS -locations relative -no-sort -silent)
    endif()
else()
    ## Generate .qm files from translations
    if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
        qt6_add_translation(QM_FILES ${TS_FILES} OPTIONS -silent)
    else()
        qt5_add_translation(QM_FILES ${TS_FILES} OPTIONS -silent)
    endif()
endif()

if(EXISTS ${TS_FILES})
    target_sources(${PROJECT_NAME} PRIVATE ${TS_FILES})
endif()
target_sources(${PROJECT_NAME} PRIVATE ${QM_FILES})

## Translations installation
if (TRANSLATION_EMB)
    ## Generat translation qrc file to embedded the qm files
    set(QRC_TRANSLATION ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-translations.qrc)
    file(WRITE ${QRC_TRANSLATION} "<RCC>\n")
    file(APPEND ${QRC_TRANSLATION} "    <qresource prefix=\"${TRANSLATION_PATH}\">\n")
    foreach(QM_FILE ${QM_FILES})
        get_filename_component(QM_FILENAME ${QM_FILE} NAME)
        file(APPEND ${QRC_TRANSLATION} "        <file alias=\"${QM_FILENAME}\">${QM_FILE}</file>\n")
    endforeach()
    file(APPEND ${QRC_TRANSLATION} "    </qresource>\n")
    file(APPEND ${QRC_TRANSLATION} "</RCC>\n")
    file(APPEND ${QRC_TRANSLATION} "")
    # qt_add_resources(TRANSLATION_RESOURCE ${QRC_TRANSLATION})
    target_sources(${PROJECT_NAME} PRIVATE ${QRC_TRANSLATION})
else()
    ## Translations installation
    install(FILES ${QM_FILES} DESTINATION ${INSTALL_TRANSLATIONS_DIR})
endif()

# message("--------------------------------------------")
# message("Project: ${PROJECT_NAME}")
# message("Project Path: ${CMAKE_CURRENT_SOURCE_DIR}")
# foreach(TS_SOURCE ${TS_SOURCE_FILES})
#     message("  TS Sources: ${TS_SOURCE}")
# endforeach()
# message("  TS File : ${TS_FILES}")
# message("  QM File : ${QM_FILES}")
# message("  QRC File: ${QRC_TRANSLATION}")
# message("--------------------------------------------")

## Manual Target to update translation files
add_custom_target(${PROJECT_NAME}-lupdate-enus
    COMMENT "Generating translation sources"
    COMMAND Qt::lupdate
    ${TS_SOURCE_FILES}
    -ts ${TS_SOURCE_DIR}/${PROJECT_NAME}_en_US.ts
    -locations relative
    -no-obsolete
    -no-sort
    -silent
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
foreach (LANG ${LINGUAS})
    add_custom_target(${PROJECT_NAME}-lupdate-${LANG}
        COMMENT "Updating translation for ${LANG}"
        COMMAND Qt::lupdate
        ${TS_SOURCE_FILES}
        -ts ${TS_SOURCE_DIR}/${PROJECT_NAME}_${LANG}.ts
        -locations relative
        -no-sort
        -silent
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    )
    add_custom_target(${PROJECT_NAME}-lconvert-${LANG}
        COMMENT "Updating translation for ${LANG}"
        COMMAND Qt::lconvert
        -target-language ${LANG}
        -input-file ${TS_SOURCE_DIR}/${PROJECT_NAME}_en_US.ts
        -input-file ${TS_SOURCE_DIR}/${PROJECT_NAME}_${LANG}.ts
        -output-file ${TS_SOURCE_DIR}/${PROJECT_NAME}_${LANG}.ts
        -locations relative
        -no-obsolete
        -verbose
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    )
endforeach()

unset(TS_SOURCE_FILES)
